package com.wlh.animation;/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

/**
 * Created by weilh on 2016/1/26.
 */
public class ScrollListView extends ListView{

    private ListScrollListener mListener;
    float mDownY;
    public ScrollListView(Context context) {
        super(context);
    }

    public ScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public interface  ListScrollListener {
        void scroll(float y);
        void scrollEnd();
    }

    public void setListener( ListScrollListener listener) {
        mListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
                if (ev == null) return false;
                if (mListener == null) return super.onTouchEvent(ev);
                switch (ev.getAction()) {
                    case MotionEvent.ACTION_DOWN :
                        mDownY = ev.getRawY();
                        break;
                    case MotionEvent.ACTION_UP :
                        mListener.scrollEnd();
                        break;
                    case MotionEvent.ACTION_MOVE :
                        if (Math.abs(ev.getRawY() - mDownY) < 3) return super.onTouchEvent(ev);
                        mListener.scroll(ev.getRawY() - mDownY);
                        mDownY = ev.getRawY();
                }
        return super.onTouchEvent(ev);
    }

}